package arkham.data.util;

import arkham.data.Board;
import arkham.data.Board.Location;
import arkham.data.LocationContainer;

/**
 * This is a utility class for matching "location" type tags in
 * XML files
 * @author ctheng
 *
 */
public class LocationMatcher
{
	private String prefix;
	private String suffix;

	/**
	 * Creates a new location matcher with the specified matching string
	 * @param match the matching string
	 */
	public LocationMatcher(String match)
	{
		if (match.indexOf(":") == -1)
		{
			prefix = "";
			suffix = match;
		}
		else
		{
			prefix = match.substring(0, match.indexOf(":"));
			suffix = match.substring(prefix.length() + 1);
		}
	}

	/**
	 * Checks if this location pattern matches a particular location container
	 * @param lc The location container
	 * @return true if the location container matches the pattern
	 */
	public boolean matches (LocationContainer lc)
	{
		if (prefix.equals(""))
		{
			return lc.getName().equals(suffix);
		}
		else if (prefix.equals("Area"))
		{
			// possible values: Streets, Locations, Sky, Outskirts
			if (suffix.equals("Streets"))
			{
				if (lc instanceof Location)
				{
					return ((Location)lc).isStreet();
				}
				return false;
			}
			else if (suffix.equals("Locations"))
			{
				if (lc instanceof Location)
				{
					return !((Location)lc).isStreet();
				}
				return false;
			}
			else if (suffix.equals("Sky"))
			{
				return lc.getName().equals(Board.SKY_NAME);
			}
			else if (suffix.equals("Outskirts"))
			{
				return lc.getName().equals(Board.OUTSKIRTS_NAME);
			}
			else
			{
				return false;
			}
		}
		else if (prefix.equals("Neighborhood"))
		{
			if (lc instanceof Location)
			{
				return ((Location)lc).getNeighborhood().equals(suffix);
			}
			return false;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Gets the prefix
	 * @return the prefix
	 */
	public String getPrefix()
	{
		return prefix;
	}

	/**
	 * Gets the suffix
	 * @return the suffix
	 */
	public String getSuffix()
	{
		return suffix;
	}

	@Override
	public boolean equals(Object o)
	{
		if (!(o instanceof LocationMatcher)) return false;
		LocationMatcher lm = (LocationMatcher)o;
		return prefix.equals(lm.getPrefix()) && suffix.equals(lm.getSuffix());
	}
}
